# -*- coding: utf-8 -*-

import numpy as np
from tensorflow.keras.models import load_model
from 通用 import config
from 数据处理 import 数组加载, csv文件加载, csv文件加载_dataset

if __name__ == '__main__':
    print('---数据加载路径:', config.预测数据)
    print('---模型为: ' + config.model_name)

    # 加载预测数据
    train_dataset, validate_dataset, shape , classnum = csv文件加载_dataset.load(config.训练数据, 3, "纵向", "线性")

    # 加载模型
    model = load_model(config.model_name)
    scores = model.evaluate(train_dataset)
    print('Huber损失:', scores[0])
    print('平均绝对值误差:', scores[1])

    # 预测
    y = model.predict(validate_dataset)

    # 获取真实值y_true
    y_true = np.array([])
    for batch in validate_dataset:
        inputs, targets = batch
        y_true = np.append(y_true, targets.numpy())
    # array转list打印可以防止自动换行
    y = np.around(y[:,0].tolist(), decimals=2).tolist()
    y_true = y_true.tolist()
    # 每组30个, 分批计算误差
    error = []
    batch = 100
    abs_error_batch = []
    for i in range(len(y)):
        error.append(round(abs(y[i] - y_true[i]), 3))
        if i % batch == 0:
            if i != 0:
                abs_error_batch.append(error_batch)
            error_batch = 0
        error_batch = error_batch + abs(y[i] - y_true[i])

    print("\033[34m预测的值:", y)
    print("\033[32m实际的值:", y_true)
    print("\033[32m绝对值误差:", error)

    分批平均绝对误差 = ""
    for i in range(len(abs_error_batch)):
        分批平均绝对误差 = 分批平均绝对误差 + str(abs_error_batch[i]/batch) + ", "
    print("\033[33m分批平均绝对误差:", 分批平均绝对误差)
    print("\033[33m总平均绝对误差:", np.sum(error)/len(y))

    # 转化成二分类问题计算准确率 较前一天涨/跌
    # error = 0
    # for i in range(len(y_true)):
    #     if i == 0:
    #         continue
    #     y_c = y[i] > y_true_c[i - 1]
    #     y_true_c = y_true_c[i] = y_true_c[i - 1]
    #     if y_c != y_true_c:
    #         error = error + 1
    # print("\033[33m涨跌分类准确率:", error/len(y_true) * 100, "%")
